001 /*
002 * Copyright 2005 Stephen J. McConnell
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.tools.checkstyle;
020
021 import java.io.File;
022 import java.net.URI;
023 import java.net.URL;
024
025 import com.puppycrawl.tools.checkstyle.CheckStyleTask;
026
027 import net.dpml.library.Resource;
028 import net.dpml.library.Module;
029
030 import net.dpml.tools.Context;
031
032 import org.apache.tools.ant.BuildException;
033 import org.apache.tools.ant.types.FileSet;
034
035 /**
036 * The checkstyle task handes the establishment of a classic checkstyle task
037 * with automatic resolution of source directories. Typical usage is within
038 * a build file that aggregates results for a module.
039 *
040 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
041 * @version 1.0.0
042 */
043 public class CheckstyleTask extends CheckStyleTask
044 {
045 /**
046 * The format property key.
047 */
048 public static final String FORMAT_KEY = "project.checkstyle.format";
049
050 /**
051 * The default format value.
052 */
053 public static final String FORMAT_VALUE = "local:format:dpml/tools/dpml";
054
055 private boolean m_init = false;
056 private Context m_context;
057
058 /**
059 * Task initialization.
060 */
061 public void init()
062 {
063 super.init();
064 if( !m_init )
065 {
066 m_init = true;
067 m_context = (Context) getProject().getReference( "project.context" );
068 if( null == m_context )
069 {
070 final String error =
071 "Missing 'project.context' reference.";
072 throw new IllegalStateException( error );
073 }
074 Resource resource = m_context.getResource();
075 addTargetToFileset( resource );
076
077 String defaultFormat = "local:format:dpml/tools/dpml";
078 String spec = m_context.getProperty( FORMAT_KEY, FORMAT_VALUE );
079 if( !spec.startsWith( "local:" ) )
080 {
081 final String error =
082 "Invalid checkstyle format uri ["
083 + spec
084 + ". The value must be a 'local:' artifact reference (e.g. "
085 + FORMAT_VALUE
086 + ").";
087 throw new BuildException( error, getLocation() );
088 }
089
090 try
091 {
092 URL url = new URI( spec ).toURL();
093 File format = (File) url.getContent( new Class[]{File.class} );
094 setConfig( format );
095 }
096 catch( Throwable e )
097 {
098 final String error =
099 "Internal error while attempting to resolve the checkstyle format property value ["
100 + spec
101 + "] to a local file.";
102 throw new BuildException( error, e, getLocation() );
103 }
104 }
105 m_init = true;
106 }
107
108 private void addTargetToFileset( Resource resource )
109 {
110 File file = resource.getBaseDir();
111 File main = new File( file, "target/build/main" );
112 if( main.exists() )
113 {
114 FileSet fileset = new FileSet();
115 fileset.setDir( main );
116 fileset.setIncludes( "**/*.java" );
117 super.addFileset( fileset );
118 }
119 if( resource instanceof Module )
120 {
121 Module module = (Module) resource;
122 Resource[] children = module.getResources();
123 for( int i=0; i < children.length; i++ )
124 {
125 Resource child = children[i];
126 addTargetToFileset( child );
127 }
128 }
129 }
130
131 private Context getContext()
132 {
133 return m_context;
134 }
135 }